package com.zhida.jiyangcenter.order

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.*
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.*
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_serve_order_details.*

@TitleBar(false)
@ContentLayout(R.layout.activity_serve_order_details)
class ServeOrderDetailsActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.title("订单详情").color(resColor(R.color.white))
            .navigationIcon(R.drawable.iv_back_fill) {
                finish()
            }
        getData()
    }

    private fun getData() {
        lifecycleScope.request({
            if (intent.getStringExtra("uuid").isNullOrEmpty())
                APIAccount.service().serveOrderInfo1(intent.getStringExtra("order_code") ?: "")
            else
                APIAccount.service().serveOrderInfo(intent.getStringExtra("uuid") ?: "")
        }, object : BaseAPI<ServeOrderInfoEntity>() {
            override fun onSuccess(t: ServeOrderInfoEntity?, state: Int, msg: String) {
                tv_shop_order_zongjia.text = "￥${t?.total_money}"
//                tv_shop_order_coupon.text="￥${t?.order?.up_money}"
                tv_shop_order_pay.text = "￥${t?.actual_payment}"
                glideImage(niv_item_buy_car, t?.serve?.picture ?: "")
                tv_item_buy_car_name.text = t?.serve?.name
                tv_item_buy_car_money.text = "￥${t?.serve?.price}"
                tv_item_num.text = "x${t?.serve?.num}"
                when (t?.state) {
                    "0" -> {
                        tv_order_type1.text = "待支付"
                        btn_operate.text = "去支付"
                        btn_operate.visible()
                        btn_operate.setOnClickListener {
                            startActivity<PayOrderActivity> {
                                putExtra("orderCode", t.order_uuid)
                                putExtra("total_money", t.actual_payment)
                                putExtra("kind", "1")
                            }
                        }
                    }
                    "1" -> {
                        tv_order_type1.text = "已取消"
                        btn_operate.gone()
                    }
                    "2" -> {
                        tv_order_type1.text = "待服务"
                        btn_operate.text = "查看服务码"
                        btn_operate.visible()
                        btn_operate.setOnClickListener {

                        }
                    }
                    "3" -> {
                        tv_order_type1.text = "待评价"
                        btn_operate.text = "去评价"
                        btn_operate.visible()
                        btn_operate.setOnClickListener {

                        }
                    }
                    "4" -> {
                        tv_order_type1.text = "已完成"
                        btn_operate.gone()
                    }
                    "5" -> {
                        tv_order_type1.text = "待退款服务"
                        btn_operate.gone()
                    }
                    "6" -> {
                        tv_order_type1.text = "已退款服务"
                        btn_operate.gone()

                    }
                    "7" -> {
                        tv_order_type1.text = "已开始服务"
                        btn_operate.visible()
                        btn_operate.setOnClickListener {
                            val dialog = NormalDialog(context)
                            dialog.content("请确保您已体验完成服务，是否确认完成？")
                                .style(NormalDialog.STYLE_TWO)//
                                .title("温馨提示")
                                .btnNum(2)
                                .btnTextColor(
                                    resColor(R.color.colorAccent),
                                    resColor(R.color.colorAccent)
                                )
                                .btnText("取消", "确认")
                                .titleTextSize(23f)
                                .showAnim(BounceTopEnter())
                                .dismissAnim(SlideBottomExit())
                                .show()

                            dialog.setOnBtnClickL(OnBtnClickL {
                                dialog.dismiss()
                            }, OnBtnClickL {
                                dialog.dismiss()
                                receivingOrder(t.order_uuid ?: "")
                            })
                        }
                    }
                    "8" -> {
                        tv_order_type1.text = "已退货"
                        btn_operate.gone()
                    }
                    "9" -> {
                        tv_order_type1.text = "已取消"
                        btn_operate.gone()
                    }
                }
            }

            override fun onError(e: APIException) {
                tipFail(e.eMessage)
            }

        })
    }

    private fun receivingOrder(orderUuid: String) {
        lifecycleScope.request({ APIAccount.service().serveOrderReceiving(orderUuid) },
            object : BaseAPI<Any>() {
                override fun onSuccess(t: Any?, state: Int, msg: String) {
                    tipSuccess(msg)
                    getData()
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }
            })
    }

    private fun refundOrder(orderUuid: String) {
        lifecycleScope.request({ APIAccount.service().serveOrderRefund(orderUuid) },
            object : BaseAPI<Any>() {
                override fun onSuccess(t: Any?, state: Int, msg: String) {
                    tipSuccess(msg)
                    getData()
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }
            })
    }

}